package at.dahu4wa.fxclient.modules.imageview;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.sun.glass.ui.Window.Level;
import com.sun.media.jfxmedia.logging.Logger;

import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Slider;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.stage.FileChooser;

public class ImageViewer extends GridPane{
	
	int vertGrid;
	int horGrid;
	Button button1;
	GridPane gridBottom;
	GridPane gridTop;
	Image image1;
	Image image2;
	ImageView imageview1;
	ImageView imageview2;
	ScrollPane panel1;
	ScrollPane panel2;
	
	
	
	
	
	public ImageViewer(){
		this.vertGrid = 1;
		this.horGrid = 0;
		this.setHgap(10);
		this.setVgap(20);
		
		this.setStyle("-fx-border: 2px solid; -fx-border-color: red;");
		
		gridTop = new GridPane();
		gridTop.setStyle("-fx-border: 2px solid; -fx-border-color: green;");
		this.add(gridTop,0,0);
		gridTop.setHgap(10);
		gridTop.setVgap(20);
		panel1 = new ScrollPane();
		panel2 = new ScrollPane();
		imageview1 = new ImageView();
		imageview1.setFitWidth(300);
		imageview1.setPreserveRatio(true);
		imageview1.setSmooth(true);
		imageview1.setCache(true);
		
		imageview2 = new ImageView();
		imageview2.setFitWidth(300);
		imageview2.setPreserveRatio(true);
		imageview2.setSmooth(true);
		imageview2.setCache(true);
		File file = new File("/Users/Martin/Documents/workspace_java/FXDemo/resource/at/dahu4wa/fxclient/modules/imageview/flower.png");
		image1 = new Image(file.toURI().toString());
		image2 = image1;
		
		
		imageview1.setImage(image1);
		imageview2.setImage(image2);
		panel1.setContent(imageview1);
		panel1.setMinSize(300, 300);
		panel2.setMinSize(300, 300);

		panel2.setContent(imageview2);
		gridTop.add(panel1,0,0);
		gridTop.add(panel2, 1, 0);
		gridBottom = new GridPane();
		this.add(gridBottom,0,1);
		gridBottom.setStyle("-fx-border: 2px solid; -fx-border-color: blue;");
		gridBottom.setHgap(10);
	    gridBottom.setVgap(10);
	    
	    Button openFileButton = new Button("Open");
	    openFileButton.setOnAction(new EventHandler<ActionEvent>() {
	    	@Override public void handle(ActionEvent e) {
	    		openImage();
	    		
	    	}
		});
		
	    gridBottom.add(openFileButton, 0, 0);
	}
	public void addParameter(String name, IFValChanged callback){
		SliderObject para = new SliderObject(name,callback);
		System.out.println(para);
		System.out.println(para.lb.getText());
		
		gridBottom.add(para,vertGrid,horGrid);
		vertGrid++;
		
		if (vertGrid >= 2){
			horGrid++;
			vertGrid = 1;
			
		}
		
	}
	
	public void openImage(){
		FileChooser fileChooser = new FileChooser();
		FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)"	, "*.JPG");
		FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)"	, "*.PNG");
		fileChooser.getExtensionFilters().addAll(extFilterJPG,extFilterPNG);
		File file = fileChooser.showOpenDialog(null);
		
		  try {
              BufferedImage bufferedImage = ImageIO.read(file);
              Image image = SwingFXUtils.toFXImage(bufferedImage, null);
              //myImageView.setImage(image);
              setImage(image);
          } catch (IOException ex) {
              //Logger.getLogger(JavaFXPixel.class.getName()).log(Level.SEVERE, null, ex);
          }
	}
	
	
	public void setImage(Image img){
		
		
		image1 = img;
		image2 = image1;
		
		
		imageview1.setImage(image1);
		imageview2.setImage(image2);
		
	}
	
	public Image getImage(){
		
		return image2;
	}
}
